package com.cmd.driver;

import java.util.Calendar;

import com.alibaba.fastjson.JSONObject;
import com.mgr.UserOrderMgr;
import com.player.DriverUser;
import com.player.User;
import com.qy.bean.OrderDriver;
import com.qy.bean.OrderInfo;
import com.qy.util.MathUtil;
import com.qy.util.StaticData;
import com.qy.util.TimeUtil;
import com.util.AbstractCommand;
import com.util.PBMessage;

/**
 * 
 * 司机点击已到达
 * 
 * @author zwl 2018年2月1日
 */
public class ArriveCMD extends AbstractCommand {

    @Override
    public void execute(User user, PBMessage packet) throws Exception {

	JSONObject reJs = new JSONObject();
	reJs.put(StaticData.reKey.cmd.getKey(),packet.getCode());
	JSONObject param = packet.getMessage();
	if (!param.containsKey("orderId")||param.containsKey("point")) {
	    reJs.put(StaticData.reKey.code.getKey(),StaticData.codeType.param_error.getType());
	    user.sendPacket(reJs);
	    return;
	}
	String point = param.getString("point");
	// 订单号
	int orderId = param.getInteger("orderId");
	OrderInfo info = UserOrderMgr.getOrderInfo(orderId);
	if (info == null) {
	    reJs.put(StaticData.reKey.code.getKey(),StaticData.codeType.not_exist.getType());
	    user.sendPacket(reJs);
	    return;
	}
	DriverUser dUser = (DriverUser) user;
	OrderDriver od = dUser.getServerOrder();
	if(od==null){
	    reJs.put(StaticData.reKey.code.getKey(),StaticData.codeType.not_exist2.getType());
	    user.sendPacket(reJs);
	    return;
	}
	
	od.setCountDownTime(TimeUtil.addTimeToDate(TimeUtil.getSysteCurTime(), 
		Calendar.MINUTE, 30));
	/*
	 * 修改订单状态
	 */
	if(info.getOrderStatus()==StaticData.OrderStatus.ACCEPT_NOT_ARRIVE.getType()){
	    info.setOrderStatus(StaticData.OrderStatus.ARRIVE_NOT_CHECK.getType());
	}else if(info.getOrderStatus()==StaticData.OrderStatus.CHECK_NOT_ARRIVE.getType()){
	    info.setOrderStatus(StaticData.OrderStatus.ARRIVE_COUNT_DOWN.getType());
	}
	UserOrderMgr.update(info);
	//当前坐标点离目标点的距离
	double distance = MathUtil.GetDistance(point, info.getFetchCoord());
	
	reJs.put("distance",distance);
	reJs.put(StaticData.reKey.code.getKey(),StaticData.codeType.ok.getType());
	user.sendPacket(reJs);
	return;
    }

}
